package com.kedsoft.andaily.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.kedsoft.andaily.common.MessageResult;
import com.kedsoft.andaily.entity.Normal;
import com.kedsoft.andaily.service.NormalService;
import com.kedsoft.andaily.vo.PageVo;
import com.kedsoft.andaily.vo.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by Administrator on 2019/5/6.
 */
@Controller
@RequestMapping("/normal")
public class NormalController {
    @Autowired
    NormalService normalService;

    @RequestMapping("index")
    public String index(){
        return "normal/index";
    }

    @RequestMapping(value = "list")
    @ResponseBody
    public PageVo<Normal> getPageList(Pagination pagination, @RequestParam(required = false) String boardname,
                                            @RequestParam(required = false) String projectcode,
                                            @RequestParam(required = false) String factoryname,
                                            @RequestParam(required = false) String routername){
        EntityWrapper<Normal> entityWrapper = new EntityWrapper<>();
        entityWrapper.like("boardname",boardname);
        entityWrapper.like("factoryname",factoryname);
        entityWrapper.like("routername",routername);
        entityWrapper.lt("status",1);
        entityWrapper.orderBy("createtime",true);
        if(pagination.getOrder()!=null) {
            if (pagination.getOrder().toLowerCase().equals("asc")){
                entityWrapper.orderBy(pagination.getSort(), true);
            }else{
                entityWrapper.orderBy(pagination.getSort(), false);
            }
        }
        Page<Normal> page = new Page<>(pagination.getPage()==null?1:pagination.getPage(),
                pagination.getRows()==null?20:pagination.getRows());

        normalService.selectPage(page,entityWrapper);
        PageVo<Normal> pageVo = new PageVo<>(page.getRecords(),page.getTotal());
        return pageVo;
    }

    @RequestMapping("lsindex")
    public String lsIndex(){
        return "normal/lsindex";
    }

    @RequestMapping(value = "lslist")
    @ResponseBody
    public PageVo<Normal> getPageLsList(Pagination pagination, @RequestParam(required = false) String boardname,
                                      @RequestParam(required = false) String projectcode, @RequestParam(required = false) String factoryname,
                                      @RequestParam(required = false) String routername){
        EntityWrapper<Normal> entityWrapper = new EntityWrapper<>();
        entityWrapper.like("projectcode",projectcode);
        entityWrapper.like("factoryname",factoryname);
        entityWrapper.like("routername",routername);
        entityWrapper.eq("status",10);
        if(pagination.getOrder()!=null) {
            if (pagination.getOrder().toLowerCase().equals("asc")){
                entityWrapper.orderBy(pagination.getSort(), true);
            }else{
                entityWrapper.orderBy(pagination.getSort(), false);
            }
        }
        Page<Normal> page = new Page<>(pagination.getPage()==null?1:pagination.getPage(),
                pagination.getRows()==null?20:pagination.getRows());

        normalService.selectPage(page,entityWrapper);
        PageVo<Normal> pageVo = new PageVo<>(page.getRecords(),page.getTotal());
        return pageVo;
    }

    @RequestMapping(value = "task")
    @ResponseBody
    public MessageResult createNromal(){
        normalService.createTask();
        return MessageResult.getSuccess("创建成功！");
    }
}
